package com.swh.cloud.biz.conf;

import javax.annotation.PreDestroy;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 对mybatis加入dataSource
 * @author sangwenhao
 * @date 2016年12月20日
 */
@Configuration
@EnableAutoConfiguration
@EnableConfigurationProperties(DataSourceProperties.class)
//@MapperScan("com.swh.cloud.biz") ////mybaits dao 搜索路径
public class MybatisDataSource {
    
    @Autowired
    private DataSourceProperties dataSourceProperties ;
    
    private final static String mapperLocation = "classpath:com/swh/cloud/biz/mybatis/*Mapper.xml" ;

    //这里使用的是tomcat.jdbc的资源池，可以换成 c3p0...
    private DataSource pool ;
    
    //使用tomcat-jdbc
    //destroyMethod 指定销毁方法
    @RefreshScope
    @Bean(destroyMethod="close")
    public javax.sql.DataSource dataSource_Tomcat(){
		DataSourceProperties config = dataSourceProperties ;
		this.pool = new DataSource();
		pool.setDriverClassName(config.getDriverClassName());
		pool.setUrl(config.getUrl());
		if(StringUtils.isNotBlank(config.getUser())){
		    pool.setUsername(config.getUser());
		}
		if(StringUtils.isNotBlank(config.getPassword())){
		    pool.setPassword(config.getPassword());
		}
		pool.setInitialSize(config.getInitialSize());
		pool.setMaxActive(config.getMaxActive());
		pool.setMaxIdle(config.getMaxIdle());
		pool.setMinIdle(config.getMinIdle());
		pool.setTestOnBorrow(config.isTestOnBorrow());
		pool.setTestOnReturn(config.isTestOnReturn());
		pool.setValidationQuery(config.getValidationQuery());
		
		return pool ;
    }
    
    private DruidDataSource dataSource ; 
    
    @Primary //使用此注解可以配置多个数据源
    @RefreshScope
    @Bean(destroyMethod="close")
    public javax.sql.DataSource dataSource(){
    	DataSourceProperties config = dataSourceProperties ;
    	DruidDataSource dataSource = new DruidDataSource();
    	dataSource.setUrl(config.getUrl());
    	dataSource.setDriverClassName(config.getDriverClassName());
    	if(StringUtils.isNotBlank(config.getUser())){
    		dataSource.setUsername(config.getUser());
		}
		if(StringUtils.isNotBlank(config.getPassword())){
			dataSource.setPassword(config.getPassword());
		}
		dataSource.setInitialSize(config.getInitialSize());
		dataSource.setMaxActive(config.getMaxActive());
//		dataSource.setMaxIdle(config.getMaxIdle());
		dataSource.setMinIdle(config.getMinIdle());
		dataSource.setTestOnBorrow(config.isTestOnBorrow());
		dataSource.setTestOnReturn(config.isTestOnReturn());
		dataSource.setValidationQuery(config.getValidationQuery());
    	
    	return dataSource ;
    }
    
    
    //创建SessionFactory
    @Bean
    public SqlSessionFactory sqlSessionFactory(){
    	try {
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			sqlSessionFactoryBean.setDataSource(dataSource());
			PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
			sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocation));
			return sqlSessionFactoryBean.getObject() ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
    
    @Bean
    public PlatformTransactionManager transactionManager(){
    	return new DataSourceTransactionManager(dataSource());
    }
    
    //预先销毁
    @PreDestroy
    public void close(){
		if(this.pool != null){
		    this.pool.close();
		}
		if(dataSource != null){
			dataSource.close();
		}
    }
    
}
